package lt.egidijus.aatest.wizards.helpers;

import java.util.Map;

import javax.swing.JOptionPane;

import lt.egidijus.aatest.cache.CacheableOption;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class ClassCacheDialog extends ATGDialog {

	public ClassCacheDialog(Shell parent, String name, CacheableOption option) {
		super(parent, name, option);
	}
	
	
	/**
	 * Creates the dialog's contents
	 * 
	 * @param shell
	 *            the dialog window
	 */
	
	public void createContents(final Shell shell) {
		shell.setLayout(new GridLayout(2, true));

		// Show the message
		final Label label = new Label(shell, SWT.NONE);
		label.setText("Total elements in cache " + option.getCache().size());
		GridData data = new GridData();
		data.horizontalSpan = 2;
		label.setLayoutData(data);

		// Display the input box

		Label label1 = new Label(shell, getStyle());
		label1.setText("Class");
		final Text classText = new Text(shell, SWT.BORDER);
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 2;
		classText.setLayoutData(data);

		Label label12 = new Label(shell, getStyle());
		label12.setText("Value");
		final Text classValue = new Text(shell, SWT.BORDER);
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 2;
		classValue.setLayoutData(data);

		final Table table = new Table(shell, SWT.CHECK | SWT.BORDER
				| SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);

		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		GridData data2 = new GridData(SWT.FILL, SWT.FILL, true, false);
		data2.horizontalSpan = 2;
		table.setLayoutData(data2);
		data2.heightHint = 100;
		final String[] titles = { "#", "Class", "Value" };
		for (int i = 0; i < titles.length; i++) {
			TableColumn column = new TableColumn(table, SWT.NONE);
			column.setText(titles[i]);
		}
		final Map<String, String> cache = option.getCache();
		updateTable(table, titles, cache, label);

		table.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {

				if (event.detail == SWT.CHECK) {

					TableItem w = (TableItem) event.item;
					table.select(Integer.parseInt(w.getText()) - 1);

					if (w.getChecked()) {

						removeKeys.add(keys.get(table.getSelectionIndex()));
					}
					if (!w.getChecked()) {
						removeKeys.remove(keys.get(table.getSelectionIndex()));
					}

				}

			}
		});
		table.addListener(SWT.MeasureItem, new Listener() {
			public void handleEvent(Event event) {
				// height cannot be per row so simply set
				// event.height = 67;

			}
		});

		table.addListener(SWT.MouseDoubleClick, new Listener() {
			public void handleEvent(Event event) {
				int e = table.getSelectionIndex();
				String key = keys.get(e);
				String value = cache.get(key);
				System.out.println("BANDY " + key);
				String key2 = JOptionPane.showInputDialog("Please enter key",
						key);
				String value2 = JOptionPane.showInputDialog(
						"Please enter value", value);
				System.out.println("K " + key2);
				System.out.println("VK " + value2);
				if (key2 != null && value2 != null) {
					cache.remove(key);
					cache.put(key2, value2);
					updateTable(table, titles, cache, label);
				}
			}
		});
		{
			Button add = new Button(shell, SWT.PUSH);
			add.setText("Add");
			data = new GridData(GridData.FILL_HORIZONTAL);
			add.setLayoutData(data);
			add.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					if (classText.getText() != null
							&& classValue.getText() != null) {
						if (classText.getText().length() > 0
								&& classValue.getText().length() > 0) {

							option.getCache().put(classText.getText(),
									classValue.getText());
							updateTable(table, titles, option.getCache(), label);
						}
					}
				}
			});
		}

		{
			Button add = new Button(shell, SWT.PUSH);
			add.setText("Remove checked");
			data = new GridData(GridData.FILL_HORIZONTAL);
			add.setLayoutData(data);
			add.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					for (String s : removeKeys) {
						cache.remove(s);
					}
					updateTable(table, titles, cache, label);

				}
			});
		}
		{
			// Create the OK button and add a handler
			// so that pressing it will set input
			// to the entered value
			Button ok = new Button(shell, SWT.PUSH);
			ok.setText("Save");
			data = new GridData(GridData.FILL_HORIZONTAL);
			ok.setLayoutData(data);
			ok.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					// input = text.getText();
					shell.close();
				}
			});
			shell.setDefaultButton(ok);
		}
		{
			// Create the cancel button and add a handler
			// so that pressing it will set input to null
			Button cancel = new Button(shell, SWT.PUSH);
			cancel.setText("Close");
			data = new GridData(GridData.FILL_HORIZONTAL);
			cancel.setLayoutData(data);
			cancel.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					// input = null;
					shell.close();
				}
			});
		}
		// Set the OK button as the default, so
		// user can type input and press Enter
		// to dismiss

	}

}